<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\module\controllers;

use app\models\MemberRecorder;
use app\models\PaylogRecorder;
use app\models\VipRecorder;

class UserController extends BaseController{

    public function actionLogin(){

        $code = \Yii::$app->request->post("code");
        if(empty($code)){
            $result = ['result'=>0,'data'=>'参数发生错误，请重新登陆'];
            return json_encode($result);
        }
        $appid = \Yii::$app->params['appid'];
        $secret = \Yii::$app->params['secret'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code ";
        $res = file_get_contents($url);
        $res = json_decode($res,true);
        if(empty($res['openid'])){
            $result = ['result'=>0,'data'=>'请使用微信重新登陆'];
            return json_encode($result);
        }
        //是否登陆过
        $model = MemberRecorder::find()->where('openid=:id',[':id' => $res['openid']])->one();
        if(!$model){
            $model = new MemberRecorder();
            $model->token = \Yii::$app->security->generateRandomString();
            $model->openid = $res['openid'];
            $model->unionid = $res['unionid'];
            $model->addtime = time();
        }
        $model->updatetime = time();
        $model->save(false);

        if($model->status){
            $result = ['result'=>0,'data'=>'登录用户被锁定，请联系客服处理'];
        }else{
            $result = ['result'=>1,'data'=>$model->token];
        }
        return json_encode($result);
    }


    public function actionInfo(){
        $token = \Yii::$app->request->post('token');
        $user = MemberRecorder::find()->where('token=:token',[':token' => $token])->one();
        if(empty($user)){
            $result = ['result'=>0,'data'=>'请先登录'];
            return json_encode($result);
        }
        $arr['user_name'] = empty($user->user_name)?\Yii::$app->params['non_name']:$user->user_name;
        $arr['user_pic'] = empty($user->user_pic)?\Yii::$app->params['base_url'].\Yii::$app->params['non_pic']:\Yii::$app->params['base_url'].$user->user_pic;
        $arr['pay_num'] = $user->pay_num;
        $arr['jf_num'] = $user->jf_num;

        $vip = VipRecorder::find()->where('id='.$user->vip)->one();
        if($vip){
            $arr['vip_name'] = $vip->vip_name;
            $arr['vip_reduce'] = $vip->vip_reduce;
        }else{
            $arr['vip_name'] = "普通";
            $arr['vip_reduce'] = 0;
        }
        $result =['result'=>1,'data'=>$arr];
        return json_encode($result);
    }


    public function actionPaylog(){
        $token = \Yii::$app->request->post('token');
        $user = MemberRecorder::find()->where('token=:token',[':token' => $token])->one();
        if(empty($user)){
            $result = ['result'=>0,'data'=>'请先登录'];
            return json_encode($result);
        }
        $list = PaylogRecorder::find()->where('user_id='.$user->id)->orderBy('addtime DESC')->limit(10)->all();
        $list_arr=[];
        foreach($list as $k=>$v){
            $str="";
            $str.=$this->get_time($v->addtime);
            $str.=$v->content;
            switch($v->pay_type){
                case 1:
                    $str.="[余额支付]";
                    $str.="￥".$v->pay_num;
                    break;
                case 2:
                    $str.="[微信支付]";
                    $str.="￥".$v->pay_num;
                    break;
                case 3:
                    $str.="[积分消费]";
                    $str.=$v->pay_num;
                    break;
            }
            $list_arr[$k]=$str;
        }
        $result = ['result'=>1,'data'=>$list_arr];
        return json_encode($result);
    }
}
